//
//  YLRegisterViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/21.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRegisterViewController.h"
#import "YLGlobeHeader.h"
#import "YLRegisterViewModel.h"
#import "YLVerificationCodeViewModel.h"
#import "SCGestureSetController.h"
#import "WHTabbarControllerConfig.h"
#import "WHProgressHUD.h"
#import "SCSecureHelper.h"
#import "WHNetworkTool.h"
#import "YLMineViewModel.h"
#import "YLSwitchRootController.h"
#import "YLImageViewViewController.h"
#import "YLPushViewModel.h"
@interface YLRegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UIView *phoneNumberLine;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIView *passwordLine;

@property (weak, nonatomic) IBOutlet UITextField *verifyCodeTextField;
@property (weak, nonatomic) IBOutlet UIView *verifyCodeLine;

@property (weak, nonatomic) IBOutlet UITextField *workerNumberTextField;
@property (weak, nonatomic) IBOutlet UIView *workerNumberLine;

@property (weak, nonatomic) IBOutlet UIButton *verificationButton;
@property (weak, nonatomic) IBOutlet UIButton *registerLoginButton;
@property (weak, nonatomic) IBOutlet UILabel *agreementLabel;
@property (nonatomic, strong) YLRegisterViewModel *registerViewModel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *offsetY;
@property (nonatomic, strong) YLVerificationCodeViewModel *vcViewModel;
@end

@implementation YLRegisterViewController

// 验证码
- (YLRegisterViewModel *)registerViewModel
{
    if (!_registerViewModel) {
        _registerViewModel = [[YLRegisterViewModel alloc] init];
    }
    return _registerViewModel;
}

// 注册
- (YLVerificationCodeViewModel *)vcViewModel
{
    if (!_vcViewModel) {
        _vcViewModel = [[YLVerificationCodeViewModel alloc] init];
        _vcViewModel.urlStr = @"Account/RegVerifyCode";
    }
    return _vcViewModel;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self textLength];
    
   // _offsetY.constant = YLOffsetY + 26;
    
    self.title = @"注册并登录";
    
    [self bindViewModel];
    
    [self lineColor];
    
    [self agreement];
    
    [self verification];
    
    [self registerLogin];
}

- (void)registerLogin
{
    __weak typeof(self) weakSelf = self;
    
    [[_registerLoginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.registerLoginButton.enabled = NO;
        
        [[weakSelf.registerViewModel.registerCommend execute:nil] subscribeNext:^(id  _Nullable x) {
            
            __weak typeof(self) weakSelf = self;
            [[[[YLMineViewModel alloc] init].myInfoCommand execute:nil] subscribeNext:^(id  _Nullable x) {
                
                
                [YLPushViewModel setUpMyPushInfo];
        
                [WHProgressHUD dismiss];
                SCGestureSetController *controller = [[SCGestureSetController alloc] init];
                controller.gestureSetType = SCGestureSetTypeSetting;
                [controller gestureSetComplete:^(BOOL success) {
                    if (success) {
                        [WHProgressHUD showSuccess:@"手势密码创建成功"];
                        [YLSwitchRootController switchRootController];
                        [YLSwitchRootController switchMineOrRealName];
                        [SCSecureHelper openGesture:YES];
                        
                    }
                }];
                
                controller.backCilck = ^{
                     [self.navigationController popViewControllerAnimated:YES];
                     [YLSwitchRootController switchRootController];
                     [YLSwitchRootController switchMineOrRealName];
                     
                };
                
                [weakSelf.navigationController pushViewController:controller animated:YES];
            }];
            
            
            
            
        }];
        
    }];
    
    
    // 4.处理登录执行过程
    [[weakSelf.registerViewModel.registerCommend.executing skip:1] subscribeNext:^(id x) {
        
        if ([x boolValue] == NO) {
            
            weakSelf.registerLoginButton.enabled = YES;
            
       
        }
        
    }];
}

#pragma mark 判断文字长度
- (void)textLength
{
    __weak typeof(self) weakSelf = self;
    
    [_phoneNumberTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            weakSelf.phoneNumberTextField.text = [weakSelf.phoneNumberTextField.text substringToIndex:11];
        }
    }];
    
    [_passwordTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 20) {
            weakSelf.passwordTextField.text = [weakSelf.passwordTextField.text substringToIndex:20];
        }
    }];
    
    [_verifyCodeTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            weakSelf.verifyCodeTextField.text = [weakSelf.verifyCodeTextField.text substringToIndex:6];
        }
    }];
    
}

- (void)agreement
{
    _agreementLabel.attributedText = [self.registerViewModel attributedStringWithString:_agreementLabel.text];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    __weak typeof(self) weakSelf = self;
    [[tap rac_gestureSignal] subscribeNext:^(id x) {        
        YLImageViewViewController *vc = [[YLImageViewViewController alloc] init];
        vc.title = @"注册协议";
        vc.imageName = @"registrationAgreemen";
        vc.height = YLScreenW * 1080 / 237.0;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    }];
    
    [self.agreementLabel addGestureRecognizer:tap];
}

#pragma mark 验证码相关
- (void)verification
{
    _verificationButton.layer.borderColor = YLMainColor.CGColor;
    __weak typeof(self) weakSelf = self;
    [[self.verificationButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf.vcViewModel.verificationCommand execute:weakSelf.phoneNumberTextField.text];
    }];
    
    [self.vcViewModel.verificationCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        [weakSelf.verificationButton setTitle:x forState:UIControlStateNormal];
    }];
    
    
    [[self.vcViewModel.verificationCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            weakSelf.verificationButton.enabled = NO;
        } else {
            weakSelf.verificationButton.enabled = YES;
        };
    }];
}

#pragma mark 线条颜色
- (void)lineColor
{
    [self setLineColor:nil];
    __weak typeof(self) weakSelf = self;
    
    [[_phoneNumberTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf setLineColor:weakSelf.phoneNumberLine];
    }];
    [[_passwordTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf setLineColor:weakSelf.passwordLine];
    }];
    [[_verifyCodeTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf setLineColor:weakSelf.verifyCodeLine];
    }];
    [[_workerNumberTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf setLineColor:weakSelf.workerNumberLine];
    }];
}



- (void)setLineColor:(UIView *)line
{
    _phoneNumberLine.backgroundColor = YLLineColor;
    _passwordLine.backgroundColor = YLLineColor;
    _verifyCodeLine.backgroundColor = YLLineColor;
    _workerNumberLine.backgroundColor = YLLineColor;
    
    if (line == nil) {
        return;
    }
    line.backgroundColor = YLMainColor;
}

- (void)bindViewModel
{
    RAC(self.registerViewModel, phoneNumber) = _phoneNumberTextField.rac_textSignal;
    RAC(self.registerViewModel, password) = _passwordTextField.rac_textSignal;
    RAC(self.registerViewModel, verifyCode) = _verifyCodeTextField.rac_textSignal;
    RAC(self.registerViewModel, workerNumber) = _workerNumberTextField.rac_textSignal;
    RAC(self.registerLoginButton, enabled) = self.registerViewModel.registerEnableSignal;
    
}

// 是否可以看见密码
- (IBAction)seePassword:(UIButton *)sender {
    sender.selected = !sender.selected;
    _passwordTextField.secureTextEntry = !sender.selected;
}

- (void)dealloc
{
   // NSLog(@"xxxx");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
